package com.atguigu.gmall.common.utils;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gulimall.user.vo.UserAuthInfoVo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author nicc
 * @version 1.0
 * @className UserAuthUtils
 * @date 2022-10-18 01:47
 *
 * 用户工具类
 * 通过springMVC提供的RequestContextHolder(请求上下文保持器)获取同一个线程中的原始请求
 */
public class UserAuthUtils {

    public static UserAuthInfoVo getUserAuthInfo(){
        //1、从同一个线程绑定中拿到老请求
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //提取出老请求中的用户标识
        String userId = request.getHeader(RedisConst.USER_ID_HEADER);
        String userTempId = request.getHeader(RedisConst.USER_TEMP_ID_HEADER);
        Long uid = null;
        try {
            uid = Long.parseLong(userId);
        }catch (Exception e){

        }

        return  new UserAuthInfoVo(uid,userTempId);
    }
}
